using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;   

public class btSimulation : MonoBehaviour {
	
	enum btBroadPhase
	{
		btDbvt = 1,
		btAS3,
		btSimple
	};
	
	public float gravity = -9.81f;
	public int solverIteration = 6;
	public float timeStep = 0.04f;
	public int maxSubSteps = 1;
	public float fixedTimeStep = 0.04f;
	
	public byte[,] name = new byte[50,50];
	public float[] time = new float[50];
	public int[] calls = new int[50];
	
	public string[] s = new string[50];
	public byte[] t = new byte[50];
	
	[DllImport ("__Internal")] private static extern ulong simulation(float timeStep, int maxSubSteps, float fixedTimeStep, byte[,] name, float[] time, int[] calls);
	[DllImport ("__Internal")] private static extern void createDiscreteDynamicsWorldWithDbvt(float gravity, int solverIteration, int _btBroadPhase);
	[DllImport ("__Internal")] private static extern void exitPhysics();
	
	//[DllImport ("__Internal")] private static extern void profiler(out float[] time, out int[] calls);
	//char[,] name = new char[50,50];

	void Awake () 
	{
		createDiscreteDynamicsWorldWithDbvt(gravity, solverIteration, (int)btBroadPhase.btAS3);
	}
	
	void Start () 
	{
		createDiscreteDynamicsWorldWithDbvt(gravity, solverIteration, (int)btBroadPhase.btAS3);
		Time.fixedDeltaTime = fixedTimeStep;
	}
	
	void Update () 
	{
		ulong elapsedtime = simulation(timeStep, maxSubSteps, fixedTimeStep, name, time, calls);


		
		for(int i= 0;i<50;i++)
		{
			for(int j=0;j<50;j++)
			{
      			t[j] = name[i,j];
			}
			s[i] = System.Text.UTF8Encoding.UTF8.GetString(t);
		}
		
		for (int i=1;i<24;i++)
		{
			print ("name: " + s[i] + " time: " + time[i] + " calls: " + calls[i]);
			print (time[i]);
		}
	}
	
	void OnApplicationQuit ()
	{
		exitPhysics();
	}
}
